package com.projecthost.wizaz.model;

import java.io.Serializable;

public class ProductBase implements Serializable {

    private static final long serialVersionUID = -8953337780255245290L;

    private final String name;

    private final Integer myHitCount;

    private final Float starRating;

    private final Integer reviewCount;

    private final Integer productId;

    public ProductBase(Integer productId, String name, Integer myHitCount, Float starRating, Integer reviewCount) {
        this.name = name;
        this.productId = productId;
        this.myHitCount = myHitCount;
        this.starRating = starRating;
        this.reviewCount = reviewCount;
    }

    public String getName() {
        return name;
    }

    public Integer getMyHitCount() {
        return myHitCount;
    }

    public Float getStarRating() {
        return starRating;
    }

    public Integer getReviewCount() {
        return reviewCount;
    }

    public Integer getProductId() {
        return productId;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((productId == null) ? 0 : productId.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        ProductBase other = (ProductBase) obj;
        if (productId == null) {
            if (other.productId != null) {
                return false;
            }
        } else if (!productId.equals(other.productId)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("ProductBase [name=");
        builder.append(name);
        builder.append(", productId=");
        builder.append(productId);
        builder.append(", myHitCount=");
        builder.append(myHitCount);
        builder.append(", starRating=");
        builder.append(starRating);
        builder.append(", reviewCount=");
        builder.append(reviewCount);
        builder.append("]");
        return builder.toString();
    }

    public static Integer getProductIdFromProductUrl(String productUrl) {
        int endIndex = productUrl.indexOf("&next=");
        return Integer.valueOf(productUrl.substring("produkt.php?produkt=".length(), endIndex));
    }

    public static String getProductUrl(int productId, int pageCount) {
        return "produkt.php?produkt=" + productId + "&next=" + pageCount;
    }

}
